package com.edu.dao;

import com.edu.entity.Order;
import com.edu.utils.JdbcHelper;

import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ZCN
 * @version 1.0
 * @date 2021/11/23 15:45
 */
public class OrderDao {
    public void insert(Order order) throws SQLException {
        String sql = "insert into ordertab(userid,ordersn,ordername,status,createtime,closetime,paytime,payfoodid,price) " +
                "values(?,?,?,?,?,?,?,?,?)";
        JdbcHelper.update(sql,order.getUserid(),order.getOrdersn(),order.getOrdername(),order.getStatus(),order.getCreatetime()
        ,order.getClosetime(),order.getPaytime(),order.getPayfoodid(),order.getPrice());
    }

    public BigInteger returnKeyAfterInsert(Order order) throws SQLException {
        String sql = "insert into ordertab(userid,ordersn,ordername,status,createtime,closetime,paytime,payfoodid,price) " +
                "values(?,?,?,?,?,?,?,?,?)";
        Object id = JdbcHelper.insertWithReturnPrimeKey(sql, order.getUserid(), order.getOrdersn(), order.getOrdername(), order.getStatus(), order.getCreatetime()
                , order.getClosetime(), order.getPaytime(), order.getPayfoodid(), order.getPrice());
        return  (BigInteger) id;
    }

    public Order selectByid(BigInteger id) throws SQLException{
        String sql = "select * from ordertab where id = ?";
        List<Order> orders = JdbcHelper.query(sql, Order.class, Integer.valueOf(id.toString()));
        if(orders.size()>0){
            return orders.get(0);
        }
        return null;
    }

    public List<Order> selectAll() throws SQLException {
        String sql = "select * from ordertab";
        List<Order> orders = JdbcHelper.query(sql, Order.class);
        return orders;
    }


    // 完成订单(根据订单id)
    public void successorder(Integer id) throws SQLException {
        Order order = new Order();
        order.setSuccesstime(new Timestamp(new Date().getTime()));
        String sql = "update ordertab set status=2,successtime=? where id = ?";
        JdbcHelper.update(sql,order.getSuccesstime(),id);
    }

    // 取消订单
    public void cancelOrder(Integer id) throws SQLException {
        String sql = "update ordertab set status = 4 where id = ?";
        JdbcHelper.update(sql,id);

    }

    // 支付成功修改状态(根据订单号)
    public void updateAfterPay(String ordersn) throws SQLException {
        Order order = new Order();
        order.setPaytime(new Timestamp(new Date().getTime()));
        String sql = "update ordertab set status=1,paytime=? where ordersn = ?";
        JdbcHelper.update(sql,order.getPaytime(),ordersn);
    }

    // 未支付订单
    public List<Order> findnotpayByUserid(Integer userid) throws Exception {
        String sql = "select * from ordertab where userid = ? and status = 0";
        List<Order> notpayOrdersfromdb = JdbcHelper.query(sql, Order.class, userid);
        ArrayList<Order> notpayOrders = new ArrayList<>();
        for (Order notpayOrder : notpayOrdersfromdb) {
            String a = notpayOrder.getClosetime().toString();
            String s = a.replaceAll("T"," ");
            if(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s).after(new Date())){
                notpayOrders.add(notpayOrder);
            }
        }
        return notpayOrders;
    }

    // 待上菜订单
    public List<Order> findnotserveByUserid(Integer userid) throws SQLException {
        String sql = "select * from ordertab where userid = ? and status = 1";
        List<Order> notserveOrders = JdbcHelper.query(sql, Order.class, userid);
        return notserveOrders;
    }

    // 已完成订单
    public List<Order> findsuccessByUserid(Integer userid) throws SQLException {
        String sql = "select * from ordertab where userid = ? and status = 2";
        List<Order> successOrders = JdbcHelper.query(sql, Order.class, userid);
        return successOrders;
    }

    // 已取消订单
    public List<Order> findcancelByUserid(Integer userid) throws SQLException {
        String sql = "select * from ordertab where userid = ? and status = 4";
        List<Order> cancelOrders = JdbcHelper.query(sql, Order.class, userid);
        return cancelOrders;
    }


}
